package com.video.controller.webcon;

import com.qf.entity.Page;
import com.qf.entity.ResultData;
import com.video.commons.loginaop.LoginUser;
import com.video.commons.loginaop.MustLogin;
import com.video.commons.page.PageHelper;
import com.video.commons.sendaop.SocketSend;
import com.video.entity.PowerInfo;
import com.video.entity.Webcon;
import com.video.netty.send.DelTerminalSend;
import com.video.netty.send.DelWebconSend;
import com.video.netty.send.UpdateWebconUserSend;
import com.video.service.webcon.IWebconService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 *
 * 分控控制器
 *
 * @Author ken
 * @Date 2019/3/11
 * @Version 1.0
 */
@Controller
@RequestMapping("/webcon")
public class WebconController {

    @Autowired
    private IWebconService webconService;

    /**
     * 获取所有分控
     * @return
     */
    @RequestMapping("/list")
    @PageHelper
    public String list(Model model, Page page){
        //获得所有分控
        List<Webcon> webconList = webconService.list();
        //获得所有权限
        PowerInfo[] powerList = PowerInfo.values();
        model.addAttribute("list", webconList);
        model.addAttribute("powerlist", powerList);
        model.addAttribute("powerJson", PowerInfo.toJson());
        return "system/webconlist";
    }

    /**
     * 添加分控
     * @param webcon
     * @return
     */
    @RequestMapping("/insert")
    @SocketSend(sendClass = UpdateWebconUserSend.class, params = "#webcon")
    public String insert(Webcon webcon, Model model){

        if(!webcon.getAccount().matches("^[0-9a-zA-Z_\\-]+$")){
            //未匹配
            model.addAttribute("errorInfo", "分控账号只能由[数字、字母、下划线、中划线]组成");
            return "myerror";
        }

        webconService.insertOrUpdate(webcon);
        return "redirect:/webcon/list";
    }

    /**
     * 校验用户名是否唯一
     * @return
     */
    @RequestMapping("/checkAccount")
    @ResponseBody
    public ResultData<Boolean> checkAccount(String account){
        Webcon webcon = webconService.getById(account);
        return new ResultData<Boolean>().setData(webcon != null ? true : false);
    }

    /**
     * 根据用户名查询分控信息
     * @param account
     * @return
     */
    @RequestMapping("/queryByAccount")
    @ResponseBody
    public ResultData<Webcon> queryByAccount(String account){
        Webcon webcon = webconService.getById(account);
        return new ResultData<Webcon>().setData(webcon);
    }

    /**
     * 批量删除分控信息
     * @return
     */
    @SocketSend(sendClass = DelWebconSend.class, params = "#mid")
    @RequestMapping("/deleteWebcons")
    public String deleteWebcons(String[] mid){
        webconService.deleteByIds(mid);
        return "redirect:/webcon/list";
    }

    /**
     * 根据终端Id移除当前分控与终端间的关系
     * @return
     */
    @ResponseBody
    @RequestMapping("/deleteByUid")
    @MustLogin
    @SocketSend(sendClass = DelTerminalSend.class, params = {"#userid"})
    public ResultData<Integer> deleteByUid(@LoginUser Webcon webcon, Integer userid){
        int result = webconService.deleteClientUsers(webcon, userid);
        return new ResultData<Integer>().setData(result);
    }
}
